package com.example.hilt_mvvm_http

import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.hilt_mvvm_http.app.getAttrValue
import com.example.hilt_mvvm_http.base.Empty
import com.example.hilt_mvvm_http.base.Failed
import com.example.hilt_mvvm_http.base.Loading
import com.example.hilt_mvvm_http.base.Success
import com.example.hilt_mvvm_http.base.observeWithLifecycle
import com.example.hilt_mvvm_http.dialog.AppUpdateDialog
import com.example.hilt_mvvm_http.dialog.MessageNoticeDialog
import com.example.hilt_mvvm_http.viewmodel.MainViewModel
import dagger.hilt.android.AndroidEntryPoint

const val TAG = "MainActivity"
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private val mainViewModel:MainViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        mainViewModel.appUpdate() //请求数据

        mainViewModel.appUpdateShareFlow.observeWithLifecycle(this){
            when(it){
                is Loading -> {
                    Log.d(TAG, "Loading: ")
                }
                is Success -> {
                    Log.d(TAG, "Success: ")
                }
                is Failed -> {
                        Log.d(TAG, "Failed: ${it.message}")
                }
                else -> {}
            }
        }


    }

    fun showDialog(view: View) {
        appUpdateDialog()

    }
    private fun appUpdateDialog(){
        AppUpdateDialog(
            title = "关于服务器返回版本升级说明",
            message = "服务器返回数据：\n1:优化登陆\n2:添加小冰",
            force = false,
            onUpdateClick = {
                Toast.makeText(this, "点击了更新", Toast.LENGTH_SHORT)
            }
        ).show(supportFragmentManager)

    }
    private fun privacyDialog(){
        val colorMain =  getAttrValue(this,R.attr.colorTheme)
        val userAgreement = "《用户协议》"
        val privacyPolicy = "《隐私条款》"
        val content =
            "宗申骑士俱乐部非常尊重用户个人的信息保护，我们依据最新的监管要求特向您说明如下：\n1.在使用 APP过程中，我们有可能会收集、使用设备标识信息用于推送车辆提醒消息。\n2.我们会一直采取严格的安全技术保护您的个人信息安全。\n3.未经您的同意，我们不会以协议约定之外的形式和渠道获取、共享或使用您的个人信息。\n您可通过阅读完整的${userAgreement}和${privacyPolicy}来了解详细信息。请您充分阅读并理解以上协议，点击同意按钮，即表示您已同意上述协议及以上约定。"
        val spannableStringBuilder = SpannableStringBuilder(content) //富文本------
        val userAgreementStart = content.indexOf(userAgreement) //用户协议起始位置
        val userAgreementEnd = userAgreementStart + userAgreement.length  //用户协议结束位置
        val privacyPolicyStart = content.indexOf(privacyPolicy) //隐私条款起始位置
        val privacyPolicyEnd= privacyPolicyStart + privacyPolicy.length //隐私条款结束位置
        //设置用户协议的点击事件
        spannableStringBuilder.setSpan(object: ClickableSpan() {
            override fun onClick(p0: View) {//点击
                Toast.makeText(this@MainActivity,"用户协议",Toast.LENGTH_SHORT).show()
            }

            override fun updateDrawState(ds: TextPaint) {//样式
                ds.color = colorMain
                ds.isUnderlineText = false

            }
        },
            userAgreementStart,
            userAgreementEnd,
            SpannableStringBuilder.SPAN_EXCLUSIVE_INCLUSIVE
        )
        //设置隐私政策的点击事件
        spannableStringBuilder.setSpan(object: ClickableSpan() {
            override fun onClick(p0: View) {//点击
                Toast.makeText(this@MainActivity,"隐私政策",Toast.LENGTH_SHORT).show()
            }

            override fun updateDrawState(ds: TextPaint) {//样式
                ds.color = colorMain
                ds.isUnderlineText = false

            }
        },
            privacyPolicyStart,
            privacyPolicyEnd,
            SpannableStringBuilder.SPAN_EXCLUSIVE_INCLUSIVE
        )


        MessageNoticeDialog(
            title = "温馨提示",
            message = spannableStringBuilder,
            confirmText = "同意",
            cancelText = "不同意",
            onConfirmClick = {
                Log.d(TAG, "onConfirmClick: ")
            },
            onCancelClick = {
                Log.d(TAG, "onCancelClick: ")
            },
            cancelable = true,
            confirmClickDismiss = true,
            alignment = Gravity.START

        ).show(supportFragmentManager)




    }

    fun messageNoticeDialog(){
        MessageNoticeDialog(
            title = "提示",
            message = "是否退出应用？",
            confirmText = "确定",
            cancelText = "取消",
            onConfirmClick = {
                Log.d(TAG, "onConfirmClick: ")
            },
            onCancelClick = {
                Log.d(TAG, "onCancelClick: ")
            },
            cancelable = true,
            confirmClickDismiss = true

        ).show(supportFragmentManager)
    }
}